package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysSubject;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.SubjectService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.SubjectVo;
import com.github.pagehelper.PageInfo;

/**
 * 学科管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/subject", name = "学科管理")
public class SubjectController {
	
	@Autowired
	private SubjectService	subjectService;
	
	/**
	 *
	 * @apiGroup subject
	 * @api {get} /subject/listPage 获取学科列表(分页)
	 * @apiVersion 1.0.0
	 * @apiName getSubjectListPage
	 *
	 * @apiParam {Long} phaseId 学段id
	 * @apiParam {Integer} pageNum 页码
	 * @apiParam {Integer} pageSize 每页行数
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "total": 4,
		        "list": [
		            {
		                "subjectId": 1,
		                "subjectName": "语文",
		                "phaseId": 1,
		                "sortNum": 1,
		                "phaseName": "小学"
		            },
		            {
		                "subjectId": 2,
		                "subjectName": "数学",
		                "phaseId": 1,
		                "sortNum": 2,
		                "phaseName": "小学"
		            },
		            {
		                "subjectId": 3,
		                "subjectName": "英语",
		                "phaseId": 1,
		                "sortNum": 3,
		                "phaseName": "小学"
		            },
		            {
		                "subjectId": 26,
		                "subjectName": "信息",
		                "phaseId": 1,
		                "sortNum": 5,
		                "phaseName": "小学"
		            }
		        ],
		        "pageNum": 1,
		        "pageSize": 10,
		        "size": 4,
		        "startRow": 1,
		        "endRow": 4,
		        "pages": 1,
		        "prePage": 0,
		        "nextPage": 0,
		        "isFirstPage": true,
		        "isLastPage": true,
		        "hasPreviousPage": false,
		        "hasNextPage": false,
		        "navigatePages": 8,
		        "navigatepageNums": [
		            1
		        ],
		        "navigateFirstPage": 1,
		        "navigateLastPage": 1,
		        "firstPage": 1,
		        "lastPage": 1
		    }
		}
	 *
	 *
	 */
    @RequestMapping(value = "listPage", name = "学科列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/subject/listPage')")
    public RestResult getSubjectListPage(@RequestParam(name = "phaseId", required = false) Long phaseId,
    		@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) throws Exception {

    	PageInfo<SubjectVo> pageInfo = subjectService.getSubjectListPage(phaseId, pageNum, pageSize);
        return RestResult.ok(pageInfo);
    }
	

    /**
	 *
	 * @apiGroup subject
	 * @api {get} /subject/list 获取学科列表
	 * @apiVersion 1.0.0
	 * @apiName getSubjectList
	 *
	 * @apiParam {Long} phaseId 学段id
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": [
		        {
		            "subjectId": 1,
		            "subjectName": "语文",
		            "phaseId": 1,
		            "sortNum": 1,
		            "phaseName": "小学"
		        },
		        {
		            "subjectId": 2,
		            "subjectName": "数学",
		            "phaseId": 1,
		            "sortNum": 2,
		            "phaseName": "小学"
		        },
		        {
		            "subjectId": 3,
		            "subjectName": "英语",
		            "phaseId": 1,
		            "sortNum": 3,
		            "phaseName": "小学"
		        },
		        {
		            "subjectId": 26,
		            "subjectName": "信息",
		            "phaseId": 1,
		            "sortNum": 5,
		            "phaseName": "小学"
		        }
		    ]
		}
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "学科列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/subject/list')")
    public RestResult getSubjectList(@RequestParam(name = "phaseId", required = false) Long phaseId ) throws Exception {

        List<SubjectVo> subjectList = subjectService.getSubjectList(phaseId);
        return RestResult.ok(subjectList);
    }
    
  
    /**
	 *
	 * @apiGroup subject
	 * @api {post} /subject/save 保存学科信息
	 * @apiVersion 1.0.0
	 * @apiName save
	 *
	 * @apiParam {SysSubject} subject 学科信息
	 * @apiParamExample 请求示例（json）
	 * {
		
			"subjectId": 2,
			"subjectName": "数学",
		    "phaseId": "2"
		
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "subjectId": 2
		    }
		}
	 *
	 */
	@RequestMapping(value = "save", name = "保存学科信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/subject/save')")
	public RestResult save(@Valid @RequestBody SysSubject subject) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = subjectService.saveSubject(subject);
	
		return RestResult.ok(result);
	}
	

	/**
	 *
	 * @apiGroup subject
	 * @api {get} /subject/info 获取学科信息
	 * @apiVersion 1.0.0
	 * @apiName getSubject
	 *
	 * @apiParam {Long} subjectId 学科id
	 * 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "subjectId": 2,
		        "subjectName": "数学",
		        "phaseId": 1,
		        "sortNum": 2,
		        "phaseName": "小学"
		    }
		}
	 *
	 *
	 */
	@RequestMapping(value = "info", name = "学科信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/subject/info')")
	public RestResult getSubject(@RequestParam Long subjectId) throws Exception{

		SubjectVo subjectVo = subjectService.getSubjectById(subjectId);
		
		return RestResult.ok(subjectVo);
	}
	
	/**
	 *
	 * @apiGroup subject
	 * @api {post} /subject/delete 删除学科信息
	 * @apiVersion 1.0.0
	 * @apiName deleteSubject
	 *
	 * @apiParam {Map} dataMap 
	 * @apiParamExample 请求示例（json）
	 * {
		    "subjectId": 2
		}
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/delete" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/subject/delete')")
    public RestResult deleteSubject(@RequestBody Map<String,Object> dataMap) throws Exception {
 	   
    	Long subjectId = MapUtils.getLong(dataMap, "subjectId");
    	
    	if (subjectId == null || subjectId<=0)
    	{
    		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
    	}
    	    			
    	int isOk = subjectService.deleteSubject(subjectId);
    	if (isOk>0)
		{
			return RestResult.ok();
		}
		else
		{
			return RestResult.error();
		}
    }


}
